<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="选择钉钉部门" @ok="handleSubmit">

    <BasicTree toolbar search treeWrapperClassName="h-[calc(100%)] overflow-auto" :clickRowToExpand="false"
      :treeData="treeData" :fieldNames="{ key: 'deptId', title: 'name' }" @select="handleSuccess" />
  </BasicModal>
</template>

<script lang="ts" setup>
import DataList from './index.vue';
import { useModal } from "@/components/Modal";
import { ref, computed, watch, onMounted } from "vue";
import { useMessage } from "@/hooks/web/useMessage";
import { getAllDeptListApi } from '@/api/sys/sysData';
import { BasicModal, useModalInner } from '@/components/Modal';

import { BasicTree, TreeItem } from '@/components/Tree';
import { useUserStore } from '@/store/modules/user';
const userStore = useUserStore();
const treeData = ref<TreeItem[]>([]);

defineOptions({ name: 'Ainput' });
const props = defineProps({
  mode: { type: String, default: "multiple" },
  ids: { type: String, default: "" },
  isAdd: { type: Boolean, default: false },
  value: { type: String, default: "" },
});
const emit = defineEmits(['change', 'update:value', "success"]);
const { createMessage } = useMessage();
//userStore.chatIsUpdate
const isUpdate = computed(() => { return userStore.chatIsUpdate });
const options = ref([])
const txtValue = ref("");
const selectList = ref([]);
watch(() => props.value, (val) => {
  txtValue.value = val

})



const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {

  fetch();

});

async function fetch() {
  setModalProps({ confirmLoading: true });

  await getAllDeptListApi(null).then((res) => {
    if (res.code == 200) {
      treeData.value = res.data;
    } else {
      createMessage.error(res.message);
    }
  }).finally(() => {
    setModalProps({ confirmLoading: false });
  });
}


function handleSuccess(list, e2) {
  // console.log("---e2:");
  // console.log(e2);
  // selectList.value = list;
  txtValue.value = e2.node;
}
function handleSubmit() {
  if (!txtValue.value) {
    createMessage.error("请选择数据");
    return;
  }
  emit("success", txtValue.value);
  closeModal();

}
</script>

<style scoped></style>
